//
//  TJEmployeeManageController.m
//  BaoDian
//
//  Created by mac on 2024/7/27.
//

#import "TJStaffManageController.h"
#import "TJStaffManageCell.h"
#import "TJEmptyDataHandler.h"
#import "HJModifyGroupNameController.h"
#import "TJStaffListController.h"
#import "TJUIAlertController.h"

@interface TJStaffManageController ()

@property (nonatomic, strong) NSMutableArray *datas;
@property (nonatomic, strong) TJEmptyDataHandler *emptyDataHandler;

@end

@implementation TJStaffManageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationBar.title = @"员工管理";
    
    [self.tableView registerClass:TJStaffManageCell.class forCellReuseIdentifier:@"TJEmployeeManageCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.rowHeight = 242;
    self.tableView.contentInset = UIEdgeInsetsMake(16, 0, 0, 0);
    self.tableView.contentOffset = CGPointMake(0, -16);
    self.tableView.backgroundColor = self.view.backgroundColor;
    [self.tableView setSectionIndexColor:UIColor.tj_9B9B9BColor];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(KNavBarHight);
        make.left.right.bottom.offset(0);
    }];
    
    _emptyDataHandler = [TJEmptyDataHandler emptyDataHandleWithScrollView:self.tableView configuration:^TJEmptyDataType(TJEmptyDataModel * _Nonnull dataModel) {
        dataModel.text = @"你暂时还没有创建分组";
        dataModel.iconName = @"EmployeeManageEmpty";
        return TJEmptyDataTypeBase;
    }];

    [self loadData];
}

- (void)loadData {
    [TJMinePresent laodStaffManagerListComplete:^(NSArray * _Nonnull datas, NSError * _Nullable error) {
        if (datas.count) {
            [self.datas removeAllObjects];
            [self.datas addObjectsFromArray:datas];
        }
        self.emptyDataHandler.canDisplay = YES;
        [self.tableView reloadData];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TJStaffManageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TJEmployeeManageCell" forIndexPath:indexPath];
    TJWeakify(self)
    TJStaffManageModel *model = self.datas[indexPath.row];
    cell.model = model;
    [cell setDidClickGroupButtonBlock:^{
        TJStaffListController *employeeListController = [[TJStaffListController alloc] init];
        employeeListController.model = model;
        [weakself.navigationController pushViewController:employeeListController];
        [employeeListController setCreateStaffSuccess:^{
            [weakself.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        }];
    }];
    
    
    [cell setDidClickDeleteButtonBlock:^{
        
        TJUIAlertController *alert = [TJUIAlertController alertControllerWithTitle:nil message:[NSString stringWithFormat:@"是否要删除 \"%@\" 分组", model.name] preferredStyle:TJUIAlertControllerStyleActionSheet];
        [alert addAction:[TJUIAlertAction actionWithTitle:@"确认" style:TJUIAlertActionStyleDefault handler:^(TJUIAlertAction * _Nonnull action) {
            [TJHUD showLoadingInView:weakself.view];
            [TJMinePresent deleteStaffGroupWithGroupId:model.groupId complete:^(NSError * _Nullable error) {
                if (error) {
                    return;
                }
                [weakself.datas removeObject:model];
                [weakself.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            }];
        }]];
        [alert addAction:[TJUIAlertAction actionWithTitle:@"取消" style:TJUIAlertActionStyleCancel handler:^(TJUIAlertAction * _Nonnull action) {
            
        }]];
        [alert showInViewController:self];
    }];
    
    [cell setDidClickChangeNameButtonBlock:^{
        HJModifyGroupNameController *modifyGroupNameController = [[HJModifyGroupNameController alloc] init];
        modifyGroupNameController.groupId = model.groupId;
        modifyGroupNameController.name = model.name;
        [modifyGroupNameController setComplete:^(NSString *name){
            model.name = name;
            [weakself.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        }];
        [weakself presentViewController:modifyGroupNameController animated:NO completion:nil];
        
    }];
    return cell;
}

- (NSMutableArray *)datas {
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}

 

@end
